<?php
/**
 * * get produce stats
 *
 * @author     ben
 */
class getChart1DataAction extends WacStatsAction
{
    public $output;
    public $periods;
    public $statsResult;

    

    public function execute($request)
    {
//        sfConfig::set('sf_web_debug', false);
//        $this->setLayout(false);
//        $this->periods = $this->getPeriods($request);
//        return $this->renderText(print_r($this->periods,true));

//        $swfParams = $this->prepareParams($request);
//        $this->output = print_r($swfParams, true);
//

//        return $this->_outputSWF($request, $swfParams);
        return $this->_outputBar3DSWFTest($request);

//        return $this->renderText($this->output);
    }

    /*
     * prepare swf file params
     * @return array
     */
    public function prepareParams($request)
    {
        $items = array();
        $this->periods = $this->getPeriods($request);
        if(count($this->periods) > 0){
            foreach($this->periods as $period){
                $item = array();
                $item["period"] = $period;
                $item["stats"][WacGoodsCategory::$predefinedClassCodes['classDyeCode']] = 
                        Doctrine::getTable(WacTable::$stockGoodsStatement)->sumUpGoodsNumberByGoodsCategory($period, WacGoodsCategory::$predefinedClassCodes['classDyeCode']);
                $items[] = $item;
            }
        }
        return $items;
    }


    protected function _outputSWF($request, $swfParams)
    {
        $chartData = array();
        
        $swfParamsCount = count($swfParams);
        if($swfParamsCount==0)
        {
            return sfView::NONE;
        }

         //Create new stBar3D object and set the transparency and colour.
		$redBar = new stBar3D( 75, '#d01f3c' );
		$redBar->key( 'Visites', 10 );

		//2nd Bar
		$blueBar = new stBar3D( 75, '#356aa0' );
		$blueBar->key( 'Leads', 10 );

        //3nd Bar
		$greenBar = new stBar3D( 75, '#80FF00' );
		$greenBar->key( 'Ventes', 10 );

        //assign data
        $maxNumber = 0;
        for( $i = 0; $i < $swfParamsCount; $i++ )
        {
            $redBar->data[]   = $swfParams[$i]['visitor_number'];
            $maxNumber = ($swfParams[$i]['visitor_number']>$maxNumber) ? $swfParams[$i]['visitor_number'] : $maxNumber;

            $blueBar->data[]  = $swfParams[$i]['client_number'];
            $maxNumber = ($swfParams[$i]['client_number']>$maxNumber) ? $swfParams[$i]['client_number'] : $maxNumber;
            
            $greenBar->data[] = $swfParams[$i]['vehicle_sale_number'];
            $maxNumber = ($swfParams[$i]['vehicle_sale_number']>$maxNumber) ? $swfParams[$i]['vehicle_sale_number'] : $maxNumber;

            $chartXLabels[]   = $swfParams[$i]['site_name']."\n ".$swfParams[$i]['comparison_percent'];
        }
        $maxHigh = $maxNumber + (10 - $maxNumber%10);

        if($maxHigh<= 100)
        {
            $step =4;
        }
        else
        {
            $step =8 ;
        }


		$g = new stGraph();
		$g->bg_colour = '#E4F5FC';
		$g->title( 'Statistiques partenaires', '{font-size:20px; color: #18A6FF;}' );

		$g->data_sets[] = $redBar;
		$g->data_sets[] = $blueBar;
                $g->data_sets[] = $greenBar;

		//to create 3d x-axis
		$g->set_x_axis_3d( 10 );
		$g->x_axis_colour( '#8499A4', '#E4F5FC' );
		$g->y_axis_colour( '#8499A4', '#E4F5FC' );

		$g->set_x_labels( $chartXLabels );
		$g->set_y_max( $maxHigh );
		$g->y_label_steps( $step );
		$g->set_y_legend( 'Nombre', 12, '#18A6FF' );
		echo $g->render();

		return sfView::NONE;
    }

    
}